class Solution:
    def removeDuplicates(self, nums) -> int:
        now=nums[0]
        count=0
        i=0
        result=0
        while i<len(nums):
            if nums[i]==now:
                if count<2:
                    count+=1
                else:
                    go=i+1
                    while go<len(nums) and nums[go]<=now:
                        go+=1
                    if go==len(nums):
                        return i
                    nums[i],nums[go]=nums[go],nums[i]
                    now=nums[i]
                    count=1
            elif nums[i]>now:
                count=1
                now=nums[i]
            else:
                go=i+1
                if count<2:
                    while go<len(nums) and nums[go]<now:
                        go+=1
                else:
                    while go<len(nums) and nums[go]<=now:
                        go+=1                    
                if go==len(nums):
                    return i              
                if nums[go]>now:
                    now=nums[go]
                    count=1
                else:
                    count+=1
                nums[i],nums[go]=nums[go],nums[i]
            i+=1
        return len(nums)


s=Solution()
nums=[1,2,2,2,3,4,5,6]
a=s.removeDuplicates(nums)
print(nums[:a])


'''
执行用时：
56 ms
, 在所有 Python3 提交中击败了
8.35%
的用户
内存消耗：
15.1 MB
, 在所有 Python3 提交中击败了
5.11%
的用户
'''